﻿//获取机构资源
function getORGResource(orgID) {
    var thisResult = null;
    $.ajax({
        type: "post",
        async: false,
        url: "/BaseAshx/Org/OrgAshx.ashx",
        data: //发送到服务器的参数
            {
                stype: "getORGResource",
                orgid: orgID
            },
        datatype: "json",
        success: function (result) { 
            if (JSON.parse (result).code === 200) {
                thisResult = result;
            }
        },
        error: function (err) {
            if (err.status == 405)
                top.location.href = '/';
        }
    });
    return thisResult;
}
var modelApis = getBaseplatformPort() 
function getResourceWithTwoORG(userORG, otherORG) {
    var stype = null;
    var relationship = checkORGLevel(userORG, otherORG);
    switch (relationship) {
        case "upper":
            //stype = "getResource_upperORG";
            //break;
        case "peer":
            stype = "getResource_peerORG";
            break;
        case "lower":
            stype = "getResource_lowerORG";
            break;
        case "self":
            stype = "getResource_selfORG";
            break;
        default:
            return;
            break;
    }
    var thisResult = {};
    thisResult["relation"] = relationship;
    let url = ''
    let data = '' 
    otherORG == '101010' ? url = modelApis + 'resource/getResourceSelfORG' : url =  modelApis + 'resource/getResourceLowerORG'
    otherORG == '101010' ? data = {orgId: userORG} : data = {orgId: userORG,otherId:otherORG}
    $.ajax({
        type: "post",
        async: false,
        /*
            url: "/BaseAshx/Resource/ResourceAshx.ashx",
            data: //发送到服务器的参数
                {
                    stype: stype,
                    orgid: userORG,
                    other:otherORG
                },
        */ 
        url: url ,
        data: data,
        datatype: "json",
        success: function (result) {
            if (JSON.parse (result).code === 200) {
                thisResult["resource"] = JSON.parse(result).data;
            }
        },
        error: function (err) {
            if (err.status == 405)
                top.location.href = '/';
        }
    });
    return thisResult;
}
//获取字典表
function getDataDict(zdName) {
    var thisResult;
    var configInfo = InitialParameter("/Config.json"); 
    let modelApi = configInfo["modelTree"].type + '://' + configInfo["modelTree"].value 
    $.ajax({
        type: "post",
        async: false,
        //url: "/BaseAshx/Resource/ResourceAshx.ashx",
        url: modelApi + "/sw/dict/getDataDict",
        data: //发送到服务器的参数
          {
              //stype: "getDataDict",
              name: zdName
          },
        datatype: "json",
        success: function (result) {
            if (result == "null") {
                thisResult = null;
            }
            else {
                let data = JSON.parse(result)
                thisResult = data.data;
            }
        },
        error: function (err) {
            if (err.status == 405)
                top.location.href = '/';
        }
    });
    //thisResult = changeStrToJsonOrArray(thisResult, null, true, "json");
    
    //fillSelectUseDict(thisResult);
    return thisResult;
}
function getSpecialDataDict(zdType) {
    var thisResult;
    var configInfo = InitialParameter("/Config.json");  // 引入ajax请求的基路径http://192.168.100.123:8887  
    let modelApi = configInfo["modelTree"].type + '://' + configInfo["modelTree"].value   
    $.ajax({
        type: "post",
        async: false,
        //url: "/BaseAshx/Resource/ResourceAshx.ashx",
        url: modelApi + "/sw/dict/getSpecialDataDict",
        data: //发送到服务器的参数
        {
            //stype: "getSpecialDataDict",
            type: zdType
        },
        datatype: "json",
        success: function (result) {
            if (result == "null") {
                thisResult = null;
            }
            else {
                let data = JSON.parse(result);
                thisResult = data.data;
            }
        },
        error: function (err) {
            if (err.status == 405)
                top.location.href = '/';
        }
    });
    //thisResult = changeStrToJsonOrArray(thisResult, null, true, "json");
    return thisResult;
}
//获取所属单位属性下拉列表
function getCompanyDataDict(portaluserno) {
    var thisResult;
    var configInfo = InitialParameter("/Config.json");  // 引入ajax请求的基路径http://192.168.100.123:8887  
    let modelApi = configInfo["modelTree"].type + '://' + configInfo["modelTree"].value
    $.ajax({
        type: "post",
        async: false,
        url: modelApi + "/sw/dept/getDeptByUserNo",
        data: //发送到服务器的参数
        {
            userNo: portaluserno
        },
        datatype: "json",
        success: function (result) {
            if (result == "null") {
                thisResult = null;
            }
            else {               
                let data = JSON.parse(result);
                thisResult = data.data;
            }
        },
        error: function (err) {
            if (err.status == 405)
                top.location.href = '/';
        }
    });
    //thisResult = changeStrToJsonOrArray(thisResult, null, true, "json");
    return thisResult;
}
//使用字典数据填充下拉列表
function fillSelectUseDict(dataDict,selectNode) {
    var oneDataDict, oneOption;
    $(selectNode).empty();
    for (var key in dataDict) {
        oneDataDict = dataDict[key];
        $(selectNode).append("<option value='" + oneDataDict.ZDVALUE + "' title='" + oneDataDict.ZDTITLE + "'>" + oneDataDict.ZDTITLE + "</option>");
        //$(selectNode).append("<option value='" + oneDataDict.ZDVALUE + "' title='" + oneDataDict.ZDTITLE + "'>" + oneDataDict.ZDNAME + "</option>");
    }
}
//资源权限保存
function savePermission(orgID, sourceOrgID) { 
    var thisCheckResult = getCheckResult();  
    console.log(thisCheckResult,'thisCheckResult');
    thisCheckResult = getStrCheckResult(thisCheckResult);
    checkResult = {};
    var thisResult = false;
    $.ajax({
        type: "post",
        async: false,
        url: modelApis + "resource/savePermission",
        data: //发送到服务器的参数
            {
                orgID: orgID,
                sourceOrgID: sourceOrgID,
                checkresult: thisCheckResult
            },
        datatype: "json",
        success: function (result) { 
            let res = JSON.parse(result)
            if (res.code === 200) {
                thisResult = result; 
                layui.layer.msg(res.msg)
            } else { 
                layui.layer.msg(res.msg,{ time: 1000}, function(){
                    layer.closeAll() 
                })
            }
        },
        error: function (err) {
            if (err.status == 405)
                top.location.href = '/';
        }
    });
    var resultArr = thisResult.split("|");
    // if (resultArr[1]=="0") {
    //     alert("保存成功！");
    // }
    // else {
    //     alert(resultArr[0] + "条权限保存成功，" + resultArr[1] + "条权限保存失败");
    // }
    return thisResult;
}
//资源权限结果字符串
function getStrCheckResult(checkResult) {
    var thisResult = "";
    var oneCheckResult, oneStrCheckResult;
    for (var key in checkResult) {
        oneCheckResult = checkResult[key];
        oneStrCheckResult = key + "《a》" + oneCheckResult.view + "《a》" + oneCheckResult.permission;
        if (thisResult == "") {
            thisResult = oneStrCheckResult;
        }
        else {
            thisResult += "《aa》" + oneStrCheckResult;
        }
    }
    return thisResult;
}
//通过guid获取一条资源信息
function getResourceByGUID(resourceguid) {
    var configInfo = InitialParameter("/Config.json");
    var thisResult; 
    let modelApi = configInfo["modelTree"].type + '://' + configInfo["modelTree"].value 
    $.ajax({
        type: "post",
        async: false,
        url: modelApi + "/sw/resource/getResourceByGuid",
        data: //发送到服务器的参数
          {
              //stype: "getResourceByGUID",
              guid: resourceguid
          },
        datatype: "json",
        success: function (result) {
            if (result == "null") {
                thisResult = null;
            }
            else {
                let data = JSON.parse(result)
                thisResult = data.data;
            }
        },
        error: function (err) {
            if (err.status == 405)
                top.location.href = '/';
        }
    });
    //thisResult = changeStrToJsonOrArray(thisResult, null, true, "json");
    return thisResult;
}
//获取一级目录下二级目录内容按照下载量排序
function getCatalogLevelTwo(attrA) {
    var thisResult = null;
    $.ajax({
        type: "post",
        async: false,
        //url: "/BaseAshx/Resource/ResourceAshx.ashx",
        url: getBaseplatformPort() + "resource/getCatalogLevelTwo",
        data: //发送到服务器的参数
          {
              //stype: "getCatalogLevelTwo",
              levelone: attrA
          },
        datatype: "json",
        success: function (result) {
            var s = JSON.parse(result);
            if (s.code == 200) {
                thisResult = JSON.stringify(s.data);
            } else {
                thisResult = JSON.stringify([]);
            }
            //thisResult = result;
        },
        error: function (err) {
            if (err.status == 405)
                top.location.href = '/';
        }
    });
    return thisResult;
}
//用户添加、移除工具、获取工具列表
function addOrRemoveServer(opera,serverGUID,callback) {
    var thisResult = null;
    $.ajax({
        type: "post",
        async: false,
        url: "/BaseAshx/Resource/ResourceAshx.ashx",
        data: //发送到服务器的参数
            {
                stype: "serverResourceOpera",
                opera:opera,
                guid: serverGUID?serverGUID:""
            },
        datatype: "json",
        success: function (result) {
            thisResult = JSON.parse(result);
            callback && callback(thisResult);
        },
        error: function (err) {
            if (err.status == 405)
                top.location.href = '/';
        }
    });
    return thisResult;
}
// 时间戳转换时间格式
function formatDate (value) {
    if (typeof (value) == 'undefined') {
        return ''
    } else {
        let date = new Date(parseInt(value))
        let y = date.getFullYear()
        let MM = date.getMonth() + 1
        MM = MM < 10 ? ('0' + MM) : MM
        let d = date.getDate()
        d = d < 10 ? ('0' + d) : d
        let h = date.getHours()
        h = h < 10 ? ('0' + h) : h
        let m = date.getMinutes()
        m = m < 10 ? ('0' + m) : m
        let s = date.getSeconds()
        s = s < 10 ? ('0' + s) : s
        return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s
    }
}